#include "user_include.h"

static void drv_init(void);
static int8_t drv_write(uint16_t addr, uint8_t *buffer, uint8_t size);
static int8_t drv_read(uint16_t addr, uint8_t *buffer, uint8_t size);
static void drv_task(void);
#define I2C_ADDR 0XA0 // 0xA0

struct driver_at24c02_struct_def driver_at24c02_struct = {
    .init = drv_init,
    .write = drv_write,
    .read = drv_read,
    .task = drv_task,
};

static void drv_init(void)
{
}

static int8_t drv_write(uint16_t addr, uint8_t *buffer, uint8_t size)
{
    int32_t ret = 0;
    osStatus_t temp;

#if 0
    HAL_I2C_Mem_Write_DMA(&hi2c2, I2C_ADDR, addr, I2C_MEMADD_SIZE_8BIT, buffer, size);
    temp = osSemaphoreAcquire(sid_Semaphore_tx, 100);
    if (temp != HAL_OK)
    {
        ret = 1;
    }
    return ret;
#elif 0
    HAL_I2C_Mem_Write_DMA(&hi2c2, I2C_ADDR, addr, I2C_MEMADD_SIZE_8BIT, buffer, size);
    osDelay(5);
    return 0;
#else
    osSemaphoreAcquire(sem_i2c2Handle, 100);
    HAL_I2C_Mem_Write(&hi2c2, I2C_ADDR, addr, I2C_MEMADD_SIZE_8BIT, buffer, size, 10);
    osSemaphoreRelease(sem_i2c2Handle);
    return 0;
#endif
}

static int8_t drv_read(uint16_t addr, uint8_t *buffer, uint8_t size)
{
#if 0
#elif 0
    HAL_I2C_Mem_Read_DMA(&hi2c2, I2C_ADDR, addr, I2C_MEMADD_SIZE_8BIT, buffer, size);
    return 0;
#else
    osSemaphoreAcquire(sem_i2c2Handle, 100);
    HAL_I2C_Mem_Read(&hi2c2, I2C_ADDR, addr, I2C_MEMADD_SIZE_8BIT, buffer, size, 10);
    osSemaphoreRelease(sem_i2c2Handle);
    return 0;
#endif
}
#define SIZE 8
static void drv_task(void)
{
    static uint8_t table[SIZE];
    static uint8_t table1[SIZE];
    uint8_t i;
    uint8_t j = 0;
    while (1)
    {
        for (i = 0; i < SIZE; i++)
        {
            table[i] = j;
        }
        j++;
        driver_at24c02_struct.write(0x00, table, SIZE);
        osDelay(10);
        driver_at24c02_struct.read(0x00, table1, SIZE);
        osDelay(1000);
    }
}